﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;
using System.Collections.Generic;


public partial class AdminX_News_NewsMain : System.Web.UI.UserControl
{
    #region Constants
    private DataTable mCateData = new DataTable();
    protected const string HOLIZOTAL_COLUMN_NAME = "HolizotalSort";
    protected const string VERTICAL_COLUMN_NAME = "VerticalSort";
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    #endregion Constants

    protected void Page_Load(object sender, EventArgs e)
    {
       
        if(!IsPostBack){
            this.ShowMess();
            this.SetLabelLanguage();
            this.BindDataToLanguageDropDown();
            this.BindDataToDropCategory();
            this.BindDataToRepeater(1);
        }
    
        
    }

    #region Message and Label
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();

        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];

        lblCategory.Text = MultipleLanguage.LBL_CATEGORYNAME[nDefaultLanguage - 1];
        lkaddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage - 1];
        //btnSearch.Text = MultipleLanguage.BTN_SEARCH[nDefaultLanguage - 1];
    }

    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        switch (nMessage)
        {
            case 1:
                MessageBoxss.Show(MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                MessageBoxss.Show(MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:

                break;
        }
    }
    #endregion

    #region BindDataToDropDown


    private void BindDataToDropCategory()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        ddlCategory.Items.Clear();
       
        DataTable objTable = new DataTable();
        NewsCategory objNewsCategory = new NewsCategory();
        objTable = objNewsCategory.Search();
        int nCount = objTable.Rows.Count;
        FlowCategory flow = new FlowCategory(objTable, VERTICAL_COLUMN_NAME, HOLIZOTAL_COLUMN_NAME);
        flow.IDColumnName = "NewsCategoryID";
        flow.ParentIDColumName = "[Parent]";
        switch (nCurrentLanguage)
        {
            case 1:
                flow.SortColumnName = "Description1";
                flow.SortColumnName = "Description1";
                break;
            case 2:
                flow.SortColumnName = "Description2";
                flow.SortColumnName = "Description2";
                break;
            case 3:
                flow.SortColumnName = "Description3";
                flow.SortColumnName = "Description3";
                break;
        }

        flow.ParentSortDirection = "ASC";
        if (flow.Execute(0))
        {
            mCateData = flow.DestinationData();
        }
        if (mCateData != null)
        {
            foreach (DataRow dr in mCateData.Rows)
            {
                if (dr != null)
                {
                    string sText = string.Empty;
                    switch (nCurrentLanguage)
                    {
                        case 1:
                            sText = Convert.ToString(dr["Description1"]);
                            break;
                        case 2:
                            sText = Convert.ToString(dr["Description2"]);
                            break;
                        case 3:
                            sText = Convert.ToString(dr["Description3"]);
                            break;
                    }
                    Int16 iHolz = Convert.ToInt16(dr[HOLIZOTAL_COLUMN_NAME]);
                    textCreateMarginLeft(ref sText, iHolz);
                    ListItem item = new ListItem(sText, Convert.ToString(dr["NewsCategoryID"]));
                    ddlCategory.Items.Add(item);
                   
                }
            }

            Utl.SetNonSelectToControl(ddlCategory, MultipleLanguage.AllTextItem[nCurrentLanguage - 1], "0");
            
        }
        else
        {
            ddlCategory.DataSource = mCateData;
            ddlCategory.DataTextField = "ProductCategoryName";

           
            switch (nCurrentLanguage)
            {
                case 1:
                    ddlCategory.DataTextField = "Description1";
                    
                    break;
                case 2:
                    ddlCategory.DataTextField = "Description2";
                  
                    break;
                case 3:
                    ddlCategory.DataTextField = "Description3";
                   
                    break;
            }
            ddlCategory.DataValueField = "NewsCategoryID";
            ddlCategory.DataBind();
            Utl.SetNonSelectToControl(ddlCategory, MultipleLanguage.AllTextItem[nCurrentLanguage - 1], "0");

       
           
        }



    }

    private void textCreateMarginLeft(ref string text, Int16 count)
    {
        if (count > 0)
        {
            string sMargin = String.Empty;
            for (int i = 0; i < count; i++)
            {
                sMargin += "--";
                sMargin += " ";
            }
            text = sMargin + text;
        }
    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();

  
    }

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToRepeater(1);
    }

    #endregion

    #region Repeater processing

    private void BindDataToRepeater(int CurrentPageIndex)
    {
        int nItemView = 0;
        int nItemID = 0;
        string sSearch = "";
        int nCategoryID = ConvertData.ConvertToInt(ddlCategory.SelectedValue);
        int nStatus = -1;
        int nLanguageID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nPageSize = Config.PAGESIZE_MAX;
        int nPageCount = Config.PAGECOUNT;
        int nDefaultLanguage = Support.GetDefaultLanguage();

        DataTable dtbNews = new DataTable();
        News objNews = new News();
        dtbNews = objNews.Search(nItemView, nItemID,sSearch, nCategoryID, nDefaultLanguage, nLanguageID, nStatus, CurrentPageIndex, nPageSize, ref nPageCount);
        rpNews.DataSource = dtbNews;
        rpNews.DataBind();

        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage] + " " + nPageCount + " " + MultipleLanguage.LBL_FOUND[nDefaultLanguage];

    }

    protected void rpNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            int nCurrentLangID = 1;
            Label lblID = (Label)e.Item.FindControl("lblID");
            Label lblTitle = (Label)e.Item.FindControl("lblTitle");
            Label lblDateModified = (Label)e.Item.FindControl("lblDateModified");
            Label lblCategory = (Label)e.Item.FindControl("lblCategory");


            lblID.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsID"));
            lblTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsTitle"));
            lblDateModified.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CreatedDate")));
            
            int nCategoryID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsCategoryId"));

            lblCategory.Text = GetCategoryNameByID(nCategoryID, nCurrentLangID);
        }
    }

    protected void rpNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lblID = (Label)e.Item.FindControl("lblID");

        int nDefaultLanguage = Support.GetDefaultLanguage();
        string strCommand = e.CommandName;
        int nID =ConvertData.ConvertToInt(lblID.Text);
        News objNews = new News();
        switch (strCommand)
        {
            case "Delete":
                objNews.LoadById(nID);
                Support.DeleteFile("image", objNews.Data.Image);
                int nDelete = objNews.DeleteById(nID);

                BindDataToRepeater(1);
                break;

            case "Edit":
                 string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_NEWS_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;
        }
    }

    private string GetCategoryNameByID(int nCatID, int LangID)
    {
        string sCategoryName = "";
        NewsCategory objNewsCategory = new NewsCategory();
        objNewsCategory.LoadById(nCatID);
        switch (LangID)
        {
            case 1: // Vietnam
                sCategoryName = ConvertData.ConvertToString(objNewsCategory.Data.Description1);
                break;
            case 2: // English
                sCategoryName = ConvertData.ConvertToString(objNewsCategory.Data.Description2);
                break;
            case 3: // Other Lang
                sCategoryName = ConvertData.ConvertToString(objNewsCategory.Data.Description3);
                break;
        }

        return sCategoryName;


    }

    #endregion

    #region Event Click
    protected void lkaddnew_Click(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_NEWS_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindDataToRepeater(1);
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToDropCategory();
        BindDataToRepeater(1);
    }
    #endregion
}